package com.kukudexin;

/**
 * @author: 酷酷的鑫
 * @Date: 2021/12/8 13:34
 * @description:
 */
public class Test11 {
    public static void main(String[] args) {
//定义一个存储TwoDimension二维坐标的数组
        TwoDimension[] twoDimensions= {new TwoDimension(1, 2), new TwoDimension(3,4), new TwoDimension(5,6)};
        ThreeDimension[] threeDimensions = {new ThreeDimension(2,2,2),new ThreeDimension(3,3,3)};
        //定义存储FourDimension四维坐标的数组
        FourDimension[] fourArray = {new FourDimension(1,2,3,4), new FourDimension(5,6,7,8)};
        Coodinate<TwoDimension> coodinate = new Coodinate<>(threeDimensions);
        showXY(coodinate);

        Coodinate<FourDimension> fourDimensionCoodinate = new Coodinate<>(fourArray);
        showXYZ(fourDimensionCoodinate);
    }

    //定义方法显示Coodinate类中 coords数组中每个元素的x与y坐标
    public static void showXY(Coodinate<?>  c){
        System.out.println("横纵坐标:");
        for(int i = 0 ; i < c.coords.length; i++){
            System.out.println("(" + c.coords[i].x + " , " + c.coords[i].y + ")");
        }
    }

    //定义方法显示Coodinate类中 coords数组中每个元素的x,y,z坐标
    //TwoDimenstion只有x,y坐标,没有z坐标,要显示x,y,z坐标,需要使用有界通配符指定类型参数的上界为ThreeDimension
    public static void showXYZ(Coodinate<? extends ThreeDimension> c){
        System.out.println("x,y,z坐标:");
        for(int i = 0 ; i < c.coords.length; i++){
            System.out.println("(" + c.coords[i].x + " , " + c.coords[i].y + " , " + c.coords[i].z + ")");
        }
    }

    //定义坐标泛型类
    static class Coodinate< T extends TwoDimension>{    //指定泛型为TwoDimension二维坐标及它的子类
        //使用泛型坐标类定义数组
        T [] coords;

        public Coodinate(T[] coords) {
            this.coords = coords;
        }
    }

    //定义二维坐标类
    static class TwoDimension {
        int x;
        int y;

        public TwoDimension(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }
    //定义三维坐标类,继承二维坐标
    static class ThreeDimension extends TwoDimension{
        int z;

        public ThreeDimension(int x, int y, int z) {
            super(x, y);
            this.z = z;
        }
    }
    //定义四维坐标类,继承三维坐标
    static class FourDimension extends ThreeDimension{
        int t;

        public FourDimension(int x, int y, int z, int t) {
            super(x, y, z);
            this.t = t;
        }
    }
}
